<?php
include_once dirname(__FILE__).'/db/DBBlog.php';

class blog implements UserModul{
    
    private $daten;
    private $newpost = "modul/blog/new";
    private $limit = 5;
    private $position = 0;
    public  $error = false;
    private $visitorID = NULL;
    private $ownerID = NULL;
    private $overview_description = 0;
    
	public function __construct($design = NULL){
	    $this->daten = $design;		
	}
    public function Main(){		
        $this->getSetting($this->daten->User_Id);
        
        $this->daten->design->assign("paginator_aktiv",$this->position);
        $this->daten->design->assign("paginator",$this->getPaginator($this->daten->User_Id));
                
        $this->daten->design->assign("modul",DBBlog::getBlogs($this->daten->User_Id));
	}
	public function Progress(){		
		
        $this->daten->assign ('css', "Modul");
        $this->daten->assign ('js', "jsModulList");
        
        $DBpage = new Db_user_page_explorer();
        $page = $DBpage->getModulPage($this->daten->user_id, $this->daten->Modul_nr);

        $DBpageaccess = new Db_user_page_access();
        $useraccess = $DBpageaccess->getAccessbyModul($page->id);
        
    	$this->daten->assign ('useraccess',$useraccess);
    	$this->daten->assign ('modulList',"fdf");
		$this->daten->draw( 'modul/blog/progress' );
		
		
	}
    public function Widget(){
		return DBNavigator::getNavi($this->daten->User_Id,$this->daten->page);
	}
	public function getRequest($design, $page){
	    /*if(isset($design->get->del)){
	        DBGuestbook::delComment($design->var['visitorID'],$design->get->del);
	    }*/
	    if(isset($design->get->new)){
	        $page->underpage = 999999;
	        $page->link = $this->newpost;
	    }
	    if(isset($design->get->page)){
	        $this->position = $design->get->page;
	    }
	    
	   /* if(isset($design->post->newpostTitel) && isset($design->post->newpostText)){
	        $this->getSetting();
	        if(DBGuestbook::checkInsertBlock($page->user_id,$this->insertTimeBlock)){	        
	            DBGuestbook::setPost($page->user_id,$design->post->newpostTitel,$design->post->newpostText,$this->visitorID,$design->post->newpostFromName,$design->post->newpostFromEmail);
	        }else{	            
	            $this->error = "Nanu das war zu schnell, erst wieder in $this->insertTimeBlock Minuten eintrag möglich";
	        }	        
	    }*/
	    
	    return $page;
	}
    public function getPaginator($userid){
	    $counter = 0;
	    $pagiLimit = 3;
	    $tmp = 999;
	    $final = array();
	    $total = DBBlog::getCount($userid)-1;
	    $totalPages = $total / $this->limit;
	    
	    if(0 < $this->position){
	        for ($y = $this->position;0 < $y ;$y = $y - 1){
	           $counter++;
	           array_push($final, $y);
	           if($counter == $pagiLimit){
	               break;
	           }
	        }	       
	    }
	    $counter = 0; 	    
	    if($totalPages < $this->position){
	        for ($x = $this->position;$x > 1;$x--){
	           $counter++;
	           array_push($final, $x);
	           if($counter == $pagiLimit){
	               break;
	           }
	        }
	    }
	    $counter = 0;
	    
	    if($this->position == 0){
	        $this->position = 1;
	        $tmp = 0;
	    }
	    
	    if($totalPages > $this->position){
	        for ($t = $this->position;$totalPages >=$t;$t++){
	           $counter++;
	           array_push($final, $t);
	           if($counter == $pagiLimit){
	               break;
	           }
	        }
	    }
	    
	    if($tmp == 0){	        
	        $this->position = 0;
	    }	
	        $final = array_flip($final);
	    ksort($final);
	    
	    foreach ($final as $key => $pagi){
	        $final[$key] = "?page=".$key;
	    }
	    return $final;	   
    }
    public function getSetting(){
        $DbSetting = DBBlog::getSetting($this->daten->User_Id);
        $this->limit = $DbSetting->limit;
        $this->overview_description = $DbSetting->overview_description;
    }
    public function getDbSetting(){
        $DbSetting = DBBlog::getSetting($this->daten->var["UserData"]->id);
        return $DbSetting;
    }
    public function updateDbSetting($user_id,$value){
        $DbSetting = DBBlog::updateDBSetting($user_id,$value);
        return $DbSetting;
    }
    public function setSetting(){
         if(DBBlog::setDbSetting($this->daten->var["UserData"]->id)){  
            $value = array();
            $value['name'] = "My Blog";	  
            $value['slogen'] = "jeden tag aktuell";
            $value['titel'] = "Mein Bog";
            $value['url'] = "blog.html";	  
            $value['aktiv'] = 0;	
            $value['zugriff'] = 0;		
            $value['navi'] = 0;	        
            $value['navi_posi'] = 1;
            $value['main_flag'] = 0;

            return $value;
        }else{
            return false;
        }
    }
}
?>